<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIHeadStart Date Helpers
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */
 
// --------------------------------------------------------------------

/**
 * Returns the months menu in a variety of formats
 *
 * @param string $mode  Can be 'three_letter', 'numeric', 'full', or 'all'
 * @param boolean $lowercase
 */
function months_array($mode = 'three_letter', $lowercase = TRUE)
{
	$three_letter = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	$full = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	$numeric = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
		
	if ($mode == 'three_letter')
	{
		if ($lowercase)
		{
			foreach($three_letter as &$l)
				$l = strtolower($l);
		}
		
		return $three_letter;
	}
	
	if ($mode == 'full')
	{
		if ($lowercase)
		{
			foreach($full as &$l)
				$l = strtolower($l);
		}
		
		return $full;
	}
	
	if ($mode == 'numeric')
	{
		return $numeric;
	}
	
	if ($mode == 'all')
	{
		if ($lowercase)
		{
			foreach($three_letter as &$l)
				$l = strtolower($l);
				
			foreach($full as &$l)
				$l = strtolower($l);			
		}
		
		return array_merge($three_letter, array_merge($full, $numeric));
	}
}

// --------------------------------------------------------------------

/**
 * Returns the numeric representation of a month if you sent it a
 * correctly-spelled month name or three-character month abbreviation
 *
 * @param string $month_name
 * @return int
 */
function month_name_to_number($month_name)
{
	$array_one = months_array('three_letter', TRUE);
	$array_two = months_array('full', TRUE);
	
	$month_name = strtolower($month_name);
	
	if (in_array($month_name, $array_one))
		return key_for_value($array_one, $month_name) + 1;
	elseif (in_array($month_name, $array_two))
		return key_for_value($array_two, $month_name) + 1;
	else
		return FALSE;
}

// --------------------------------------------------------------------

/**
 * Checks to see if a year is valid, and optionally within a specified
 * range.
 *
 * @param string $test_year
 * @param string $earliest_year
 * @param string $latest_year
 * @return boolean
 */
function valid_year($test_year, $earliest_year = NULL, $latest_year = NULL)
{
	if (is_numeric($test_year) && ! strpos($test_year, '.'))
	{
		if (($earliest_year !== NULL) && ((int) $test_year < $earliest_year))
			return FALSE;
		if (($latest_year !== NULL) && (int) $test_year > $latest_year)
			return FALSE;
		
		//If here
		return TRUE;
	}
	else
		return FALSE;
}

// --------------------------------------------------------------------

/**
 * Checks to see if a month is valid.
 *
 * @param string|int $test_month You can send month names, three-character abbreviations, or month numbers
 * @return boolean
 */
function valid_month($test_month)
{		
	if (in_array(strtolower($test_month), months_array('all', TRUE)))
		return TRUE;
	else
		return FALSE;
}

// --------------------------------------------------------------------

/**
 * Checks to see if date information is valid
 *
 * This function will return FALSE if invalid days are sent (e.g. March 43),
 * or if invalid months are sent (month = 14 or "Maybruary"), or invlad years
 * are sent (-1230)
 *
 * @param int $test_day
 * @param string|int $test_month
 * @param int $test_year
 * @return boolean
 */
function valid_date($test_day, $test_month, $test_year)
{
	if (is_numeric($test_day) && ! strpos($test_day, '.'))
	{
		if (valid_month($test_month) && valid_year($test_year))
		{
			if ( ! is_numeric($test_month))
				$test_month = month_name_to_number($test_month);
			
			$days_in_month = days_in_month($test_month, $test_year);
			
			if ($test_day < 0 OR $test_day > $days_in_month)
				return FALSE;
			else
				return TRUE;
		}
		else
			return FALSE;
	}
	else
		return FALSE;
}

// --------------------------------------------------------------------

/**
 * Calculates a person's current age in years based on birthdate
 *
 * This function accepts any string that can be succesfully parsed by
 * the built-in PHP function strtotime()
 *
 * @see strtotime()
 * @param string $birthdate
 * @return string
 */
function calculate_age($birthdate)
{
	if ( ! is_valid_date($birthdate))
		return FALSE;

	$the_timestamp = strtotime($birthdate);
	$curr_timestamp = time();

	return number_format((($curr_timestamp - $the_timestamp) / 31536000), 0, '.', ',');
}

/* End of file MY_date_helper.php */
/* Location: ./app/helpers/MY_date_helper.php */